﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using GPS.GPS_DAL;
using System.Globalization;
using GPS.Models;


namespace GPS.Controllers.GPS_NghiepVu
{
    public class GPS_LichHoc_LTController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();

            string userId = User.Identity.GetUserId();

            ApplicationUser _currentUser = dbUser.Users.Find(userId);

            return _currentUser;
        }

        //
        // GET: /GPS_LichHoc/

        public ActionResult Index(ViewModelDKH_LH1 SearchModel)
        {
            SearchModel.KH_LH_DVGTVT_MA = this.getCurrentUser().MA_TINHTP;

            initialCategorySearchAction(SearchModel);
            return View();
        }

        //
        // Search: /GPS_LichHoc/

        public ActionResult Search(ViewModelDKH_LH1 SearchModel)
        {
            if (SearchModel == null)
                return View(SearchModel);

            if (SearchModel.KH_LH_DVGTVT_MA == null)
                SearchModel.KH_LH_DVGTVT_MA = "40";

            string _DVGTGT = this.getCurrentUser().MA_TINHTP;

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH1(SearchModel.KH_LH_DVGTVT_MA,SearchModel.KH_LH_KH_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");
            var gvs = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == SearchModel.KH_LH_DVGTVT_MA).Select(
               r => new
               {
                   GV_MA = r.GV_MA,
                   GV_TEN = r.GV_HODEM + " " + r.GV_TEN,
               });
            ViewBag.GV_MAs = new SelectList(gvs, "GV_MA", "GV_TEN"); 

            var htdts = _db.GetAll<SHTDAOTAO>();

            ViewBag.HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(o => o.HANGDT_RANK > 4), "HANGDT_MA", "HANGDT_TEN");

            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_KH_LH_1_LT(SearchModel.KH_LH_DVGTVT_MA
                                                                                , SearchModel.KH_LH_KH_MA
                                                                                , SearchModel.KH_LH_GV_MA
                                                                                , SearchModel.KH_LH_PT_BIENSO
                                                                                , SearchModel.HANGDT_MA
                                                                                );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();

            return View(SearchModel);
        }

        #region"Ajax"
        [HttpPost]
        public ActionResult getKH_MAs2(string ma_DVGT)
        {
            var districts = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN(ma_DVGT).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_TEN + "</option>");
            var giangvien = _db.Filter<DGV>(o=>o.GV_DVGTVT_MA==ma_DVGT).Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM+" "+a.GV_TEN + "</option>");
         
            var data = new JsonStringLT();
            data.strKhoaHoc = string.Join("", districts);
            data.strgiangvien = string.Join("", giangvien);
            return Json(new { pts = data }, JsonRequestBehavior.AllowGet);
            //return Content(content);
        }

        public ActionResult getGV_IN_KH_MA_LTs2(string Ma_DVGT, string Ma_KH)
        {
           
            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_KH_LT(Ma_DVGT, Ma_KH);

            var lstPTinKH = new List<JsonDataDKH_GVinKH_LT>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_GVinKH_LT>(ds.Tables[1]);
            var districts = lstPTinKH.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HOTEN + "</option>");

            var objKH = new JsonDataDKH_GVinKH_LT_1();

            objKH.OptionsGV = string.Join("", districts);

            return Json(new { pts = objKH }, JsonRequestBehavior.AllowGet);
        }


        #endregion

        public ActionResult List(string KH_LH_DVGTVT_MA, string KH_LH_KH_MA, string KH_LH_GV_MA, string KH_LH_PT_BIENSO, string HANGDT_MA, int? Page)
        {
            ViewModelDKH_LH1 SearchModel = new ViewModelDKH_LH1();
            if (string.IsNullOrWhiteSpace(KH_LH_DVGTVT_MA))
                KH_LH_DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            SearchModel.KH_LH_DVGTVT_MA = KH_LH_DVGTVT_MA;
            SearchModel.KH_LH_KH_MA = KH_LH_KH_MA;
            SearchModel.KH_LH_GV_MA = KH_LH_GV_MA;
            SearchModel.KH_LH_PT_BIENSO = KH_LH_PT_BIENSO;
            SearchModel.HANGDT_MA = HANGDT_MA;
            SearchModel.Page = Page;

            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_KH_LH_1_LT(SearchModel.KH_LH_DVGTVT_MA
                                                                                , SearchModel.KH_LH_KH_MA
                                                                                , SearchModel.KH_LH_GV_MA
                                                                                , SearchModel.KH_LH_PT_BIENSO
                                                                                , SearchModel.HANGDT_MA
                                                                                );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();
            return PartialView("_List", SearchModel);
        }



        [HttpGet]
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            CrudModelDKH_LH1 model = new CrudModelDKH_LH1();
            string _dvgt_ma = this.getCurrentUser().MA_TINHTP;
            return PartialView("_Create", model);
        }

        [HttpPost]
        public ActionResult Create(CrudModelDKH_LH1 model, FormCollection _form)
        {
            if (ModelState.IsValid)
            {
                if (model.KH_LH_GDOAN_MA == "TH" || model.KH_LH_GDOAN_MA == "TD")
                {
                    foreach (var _key in _form.AllKeys)
                    {
                        if (_key.StartsWith("chk_"))
                        {
                            string _KH_LH_PT_BIENSO = _form[_key].Split('#')[0];
                            string _KH_LH_GV_MA = _form[_key].Split('#')[1];

                            DKH_LH1 _model = new DKH_LH1();
                            GPS.COMMON.Helpers.CopyObject<DKH_LH1>(model, ref _model, new List<string> { "KH_LH_ACTIVE" });
                            _model.KH_LH_ACTIVE = "1";
                            _model.KH_LH_PT_BIENSO = _KH_LH_PT_BIENSO;
                            _model.KH_LH_GV_MA = _KH_LH_GV_MA;

                            //get max lich học
                            _model.ID = EFSequence.GetNextValue("SEQ_DKH_LH1_ID");
                            _model.KH_LH_NGUOINHAP = User.Identity.Name;
                            _model.KH_LH_NGAYNHAP = DateTime.Now;
                            _db.Insert<DKH_LH1>(_model);

                        }

                    }
                }
                else
                {
                    DKH_LH1 _model = new DKH_LH1();
                    GPS.COMMON.Helpers.CopyObject<DKH_LH1>(model, ref _model, new List<string> { "KH_LH_ACTIVE" });
                    _model.KH_LH_ACTIVE = "1";

                    //get max lich học
                    _model.ID = EFSequence.GetNextValue("SEQ_DKH_LH1_ID");
                    _model.KH_LH_NGUOINHAP = User.Identity.Name;
                    _model.KH_LH_NGAYNHAP = DateTime.Now;
                    _db.Insert<DKH_LH1>(_model);
                }


                return Json(new { success = true });

            }
            initialCategoryCreateAction();
            return Json(model, JsonRequestBehavior.AllowGet);

        }

        public ActionResult Edit(decimal id)
        {

            DKH_LH1 model = _db.GetOne<DKH_LH1>(o => o.ID == id);

            CrudModelDKH_LH1 _model = new CrudModelDKH_LH1();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LH1>(model, ref _model, new List<string> { "KH_LH_ACTIVE" });

            initialCategoryEditAction(_model);
            return PartialView("_Edit", _model);

        }

        [HttpPost]
        public ActionResult Edit(CrudModelDKH_LH1 model)
        {
            if (ModelState.IsValid)
            {

                var _model = _db.GetOne<DKH_LH1>(o => o.ID == model.ID);
                GPS.COMMON.Helpers.CopyObject<DKH_LH1>(model, ref _model, new List<string> { "KH_LH_ACTIVE" });

                _db.Update<DKH_LH1>(_model);

                return Json(new { success = true });

            }
            initialCategoryEditAction(model);
            return Json(model, JsonRequestBehavior.AllowGet);

        }

        public ActionResult Delete(decimal id)
        {

            DKH_LH1 model = _db.GetOne<DKH_LH1>(o => o.ID == id);

            CrudModelDKH_LH1 _model = new CrudModelDKH_LH1();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LH1>(model, ref _model, new List<string> { "KH_LH_ACTIVE" });

            initialCategoryEditAction(_model);
            return PartialView("_Delete", _model);
        }


        //
        // POST: /Phone/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            _db.Delete<DKH_LH1>(o => o.ID == id);
            return Json(new { success = true });
        }


        #region detail
        public ActionResult Detail(decimal id)
        {

            DKH_LH1 model = _db.GetOne<DKH_LH1>(o => o.ID == id);

            CrudModelDKH_LH1 _model = new CrudModelDKH_LH1();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LH1>(model, ref _model, new List<string> { "KH_LH_ACTIVE" });

            initialCategoryEditAction(_model);
            return PartialView("_Detail", _model);
        }
        public void initialCategoryDetailAction(CrudModelDKH_LH1 view)
        {
            ViewBag.dvgtvt = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == view.KH_LH_DVGTVT_MA && o.DVGTVT_ACTIVE=="1").Select(x => x.DVGTVT_TEN).FirstOrDefault();
            ViewBag.khoahocs = _db.Filter<DKH>(o => o.KH_MA == view.KH_LH_KH_MA).Select(x => x.KH_TEN).FirstOrDefault();

            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            var htdt = _db.Filter<SHTDAOTAO>(o => o.HTDT_MA == view.KH_LH_HTDT_MA).Select(x => x.HTDT_MA).FirstOrDefault();
            view.KH_LH_HTDT_MA = htdt.ToString();


            ViewBag.KH_LOP_LH_GDOAN_MAs = _db.Filter<SGIAIDOAN>(x => x.GDOAN_MA == view.KH_LH_GDOAN_MA).Select(x => x.GDOAN_MA).FirstOrDefault();

            ViewBag.PT_MAs = CategoryCommon.DEFAULT_VALUE_DDL;
            if (view.KH_LH_HTDT_MA != null && view.KH_LH_HTDT_MA != "LT")
            {
                ViewBag.PT_MAs = _db.Filter<DPT>(o => o.PT_DVGTVT_MA == view.KH_LH_DVGTVT_MA).Select(x => x.PT_BIENSO).FirstOrDefault();
            }

            //GV
            var gvs = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == view.KH_LH_DVGTVT_MA).Select(
               r => new
               {
                   GV_MA = r.GV_MA,
                   GV_TEN = r.GV_HODEM + " " + r.GV_TEN,
               }).OrderBy(r => r.GV_MA).FirstOrDefault();
            ViewBag.GV_MAs = gvs;

        }

        public ActionResult HocVienPhuongTien(decimal id, string id2)
        {
            string _KH_LH_KH_MA = id2.Replace("%3A", ":");
            var query =
                      from kh_pt in _db.GetDbSet<DKH_PT>()
                      join hv in _db.GetDbSet<DHV>() on kh_pt.KH_PT_HV_MA equals hv.HV_MA
                      where (kh_pt.KH_PT_KH_LH_ID == id && kh_pt.KH_PT_PT_MA == _KH_LH_KH_MA)
                      select new
                      {
                          hv.ID,
                          hv.HV_MA,
                          hv.HV_DVGTVT_MA,
                          hv.HV_KH_MA,
                          hv.HV_HANGDT_MA,
                          hv.HV_HANGDT_RANK,
                          hv.HV_HODEM,
                          hv.HV_TEN,
                          hv.HV_GIOITINH,
                          hv.HV_QUOCTICH_MA,
                          hv.HV_NGAYSINH,
                          hv.HV_NOITT,
                          hv.HV_NOITT_DVHC_MA,
                          hv.HV_NOITT_DVHC_DVQL,
                          hv.HV_NOICT,
                          hv.HV_NOICT_DVHC_MA,
                          hv.HV_NOICT_DVHC_DVQL,
                          hv.HV_SOCMT,
                          hv.HV_NGAYCAPCMT,
                          hv.HV_NOICAPCMT,
                          hv.HV_MOTA,
                          hv.HV_ACTIVE,
                          hv.HV_NGAYNHAP,
                          hv.HV_NGUOINHAP,
                          hv.HV_NGAYSUA,
                          hv.HV_NGUOISUA
                      };
            List<DHV> entities = new List<DHV>();
            foreach (var hv in query)
            {

                entities.Add(new DHV  //Added UsageCPU  
                {
                    ID = hv.ID,
                    HV_MA = hv.HV_MA,
                    HV_DVGTVT_MA = hv.HV_DVGTVT_MA,
                    HV_KH_MA = hv.HV_KH_MA,
                    HV_HANGDT_MA = hv.HV_HANGDT_MA,
                    HV_HANGDT_RANK = hv.HV_HANGDT_RANK,
                    HV_HODEM = hv.HV_HODEM,
                    HV_TEN = hv.HV_TEN,
                    HV_GIOITINH = hv.HV_GIOITINH,
                    HV_QUOCTICH_MA = hv.HV_QUOCTICH_MA,
                    HV_NGAYSINH = hv.HV_NGAYSINH,
                    HV_NOITT = hv.HV_NOITT,
                    HV_NOITT_DVHC_MA = hv.HV_NOITT_DVHC_MA,
                    HV_NOITT_DVHC_DVQL = hv.HV_NOITT_DVHC_DVQL,
                    HV_NOICT = hv.HV_NOICT,
                    HV_NOICT_DVHC_MA = hv.HV_NOICT_DVHC_MA,
                    HV_NOICT_DVHC_DVQL = hv.HV_NOICT_DVHC_DVQL,
                    HV_SOCMT = hv.HV_SOCMT,
                    HV_NGAYCAPCMT = hv.HV_NGAYCAPCMT,
                    HV_NOICAPCMT = hv.HV_NOICAPCMT,
                    HV_MOTA = hv.HV_MOTA,
                    HV_ACTIVE = hv.HV_ACTIVE,
                    HV_NGAYNHAP = hv.HV_NGAYNHAP,
                    HV_NGUOINHAP = hv.HV_NGUOINHAP,
                    HV_NGAYSUA = hv.HV_NGAYSUA,
                    HV_NGUOISUA = hv.HV_NGUOISUA
                }

                );


            }
            ViewBag.PT_BIENSO = id2;
            return PartialView("_HocVienPhuongTien", entities);
        }

        #endregion

        #region"Ajax"

        [HttpPost]
        public ActionResult getPT_IN_KH_MAs(string Ma_DVGT, string Ma_KH)
        {
            //get phương tiện trong khóa học
            Ma_DVGT = getCurrentUser().MA_TINHTP;

            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH(Ma_DVGT, Ma_KH);

            var lstPTinKH = new List<JsonDataDKH_PTinKH>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds.Tables[1]);
            var districts = lstPTinKH.Select(a => "<option value='" + a.PT_MA + "'>" + a.PT_MA + "</option>");

            var objKH = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds.Tables[0]);

            JsonDataDKH_PTinKH_1 data = new JsonDataDKH_PTinKH_1();
            data.Options = string.Join("", districts);
            data.KH_NGAY_KG = objKH[0].KH_NGAY_KG;
            data.KH_NGAY_BG = objKH[0].KH_NGAY_KT;
            data.OptionsWeek = this.GPS_GET_WEEK_BY_KH(Ma_KH);
            data.SO_HOVIEN = _db.GetDbSet<DHV>().Count(o => o.HV_KH_MA == Ma_KH).ToString();


            return Json(new { pts = data }, JsonRequestBehavior.AllowGet);
        }



        public ActionResult GPS_GET_DATE_BY_WEEK(string Week, string StartDate, string EndDate)
        {
            DateTime dtStartDate = DateTime.ParseExact(StartDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
            DateTime dtEndDate = DateTime.ParseExact(EndDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

            var intWeek = int.Parse(Week);
            DateRange dateRange = GPS_Library.GET_WEEK_DATERANGE(dtStartDate, dtEndDate)[intWeek];
            return Json(new { dateranges = dateRange }, JsonRequestBehavior.AllowGet);
        }

        public string GPS_GET_WEEK_BY_KH(string Ma_KH)
        {
            //get Week by khóa học
            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            var kh = _db.GetOne<DKH>(o => o.KH_DVGTVT_MA == Ma_DVGT && o.KH_MA == Ma_KH);
            var dictWeekinDate = GPS_Library.GET_WEEK_DATERANGE(kh.KH_NGAYKG.Value, kh.KH_NGAYBG.Value);
            var districts = dictWeekinDate.Keys.Select(a => "<option value='" + a.ToString() + "'>Tuần " + a.ToString() + "</option>");
            return string.Join("", districts);
        }

        public ActionResult getGV_IN_KH_MA_LTs(string Ma_DVGT, string Ma_KH)
        {
            //get phương tiện trong khóa học
            Ma_DVGT = getCurrentUser().MA_TINHTP;

            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_KH_LT(Ma_DVGT, Ma_KH);

            var lstPTinKH = new List<JsonDataDKH_GVinKH_LT>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_GVinKH_LT>(ds.Tables[1]);
            var districts = lstPTinKH.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HOTEN + "</option>");

            var objKH = GPS_Library.MapDataTableToList<JsonDataDKH_GVinKH_LT_1>(ds.Tables[0]);


            objKH[0].OptionsWeek = this.GPS_GET_WEEK_BY_KH(Ma_KH);
            objKH[0].SO_HOVIEN = _db.GetDbSet<DHV>().Count(o => o.HV_KH_MA == Ma_KH).ToString();
            objKH[0].OptionsGV = string.Join("", districts);

            return Json(new { pts = objKH[0] }, JsonRequestBehavior.AllowGet);
        }


        #endregion

        #region Function



        private bool IsBooking(CrudModelDKH_LH1 _model)
        {
            var IsBooking = false;
            if (_model != null)
            {
                IsBooking = _db.EntityExists<DKH_LH>(
                b => (b.KH_LH_GV_MA == _model.KH_LH_GV_MA || b.KH_LH_PT_BIENSO == _model.KH_LH_PT_BIENSO)
                    && b.KH_LH_TUNGAY < _model.KH_LH_DENNGAY && b.KH_LH_DENNGAY > _model.KH_LH_TUNGAY && b.ID != _model.ID
                );

            }


            return IsBooking;
        }



        public void initialCategorySearchAction(ViewModelDKH_LH1 viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => (o.DVGTVT_MAQL == viewModel.KH_LH_DVGTVT_MA || o.DVGTVT_MA == viewModel.KH_LH_DVGTVT_MA) && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH1(viewModel.KH_LH_DVGTVT_MA,viewModel.KH_LH_KH_MA);  //_db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.KH_LH_DVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            var htdts = _db.GetAll<SHTDAOTAO>();
            //  ViewBag.HTDT_MAs = new SelectList(htdts, "HTDT_MA", "HTDT_MOTA");
            ViewBag.HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(o => o.HANGDT_RANK > 4), "HANGDT_MA", "HANGDT_TEN");
        }

        public void initialCategoryCreateAction()
        {
            ApplicationUser curUser = getCurrentUser();
            string _madvgtvt = curUser.MA_TINHTP;

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == _madvgtvt && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            // ViewBag.HTDT_MAs = new SelectList(_db.GetAll<SHTDAOTAO>(), "HTDT_MA", "HTDT_MOTA");
            ViewBag.GD_MAs = new SelectList(_db.Filter<SGIAIDOAN>(o => o.GDOAN_MA != "TH" && o.GDOAN_MA != "TD"), "GDOAN_MA", "GDOAN_MOTA");

            ViewBag.HANGHT = " ";
            ViewBag.HANGGPLX = " ";
            string ma_DVGT = this.getCurrentUser().MA_TINHTP;
            ViewBag.KH_MAs = new SelectList(Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH(ma_DVGT), "KH_MA", "KH_TEN");   //new SelectList(_db.Filter<DKH>(d => d.KH_DVGTVT_MA == ma_DVGT),"KH_MA","KH_TEN");


            ViewBag.MONHOC_MAs = new SelectList(_db.GetAll<SMONHOC>(), "MONHOC_MA", "MONHOC_TEN");
        }

        public void initialCategoryEditAction(CrudModelDKH_LH1 view)
        {

            // ApplicationUser curUser = getCurrentUser();
            ApplicationUser curUser = getCurrentUser();
            string _madvgtvt = curUser.MA_TINHTP;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == _madvgtvt && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            // ViewBag.HTDT_MAs = new SelectList(_db.GetAll<SHTDAOTAO>(), "HTDT_MA", "HTDT_MOTA");
            ViewBag.GD_MAs = new SelectList(_db.Filter<SGIAIDOAN>(o => o.GDOAN_MA != "TH" && o.GDOAN_MA != "TD"), "GDOAN_MA", "GDOAN_MOTA");

            //KH_MAs
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH1(view.KH_LH_DVGTVT_MA,view.KH_LH_KH_MA); //_db.Filter<DKH>(o => o.KH_DVGTVT_MA == view.KH_LH_DVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            //PT

            ViewBag.PT_MAs = CategoryCommon.DEFAULT_VALUE_DDL;
            //var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH(_madvgtvt, view.KH_LH_KH_MA);
            //var pts = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds.Tables[1]);
            //ViewBag.PT_MAs = new SelectList(pts, "PT_MA", "PT_MA");

            ////GV
            //if (view.KH_LH_HTDT_MA != null && view.KH_LH_HTDT_MA != "LT")
            //{
            //    var gvs = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_KH_BY_HT(_madvgtvt, view.KH_LH_KH_MA, view.KH_LH_PT_BIENSO, view.KH_LH_HTDT_MA);
            //    ViewBag.GV_MAs = new SelectList(gvs, "GV_MA", "GV_HOTEN");
            //}
            //else
            //{
            var gvs = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_KH_BY_HT(_madvgtvt, view.KH_LH_KH_MA, string.Empty, view.KH_LH_HTDT_MA);
            ViewBag.GV_MAs = new SelectList(gvs, "GV_MA", "GV_HOTEN");
            //}

            ViewBag.MONHOC_MAs = new SelectList(_db.GetAll<SMONHOC>(), "MONHOC_MA", "MONHOC_TEN");

            //Week
            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            var kh = _db.GetOne<DKH>(o => o.KH_DVGTVT_MA == Ma_DVGT && o.KH_MA == view.KH_LH_KH_MA);
            var dictWeekinDate = GPS_Library.GET_WEEK_DATERANGE(kh.KH_NGAYKG.Value, kh.KH_NGAYBG.Value);


            List<SelectListItem> lstItemSelect = new List<SelectListItem>();


            dictWeekinDate.Keys.ToList().ForEach(o => lstItemSelect.Add(new SelectListItem() { Value = o.ToString(), Text = "Tuần " + o.ToString() }));
            ViewBag.WEEKS = new SelectList(lstItemSelect, "Value", "Text");


        }
        #endregion
    }

    public class JsonStringLT
    {
        public string strKhoaHoc { get; set; }
        public string strgiangvien { get; set; }

    }

}